#!/usr/bin/Rscript
##########################################################################################
# Issue Ownership and Agenda Setting in the 2019 Swiss National Elections
##########################################################################################
# Description:
# Appendix VAR Model Coeff...
##########################################################################################
# Content
##########################################################################################
# 1) Dependencies
# 2) Load Data
# 3) Output Model Coeff.
##########################################################################################
# 1) Dependencies
##########################################################################################
library(tidyr)
library(dplyr)
library(vars)
library(boot)
library(rio)
library(ggplot2)
library(ggraph)
library(ggplotify)
library(ggforce)
library(ggrepel)
library(forecast)
library(urca)
library(xtable)
library(stargazer)
library(broom)
library(knitr)
library(kableExtra)
##########################################################################################
# 2) Load Data
##########################################################################################
rm(list=ls())
# - set dir
args = commandArgs()

scriptName = args[substr(args,1,7) == '--file=']

if (length(scriptName) == 0) {
  scriptName <- rstudioapi::getSourceEditorContext()$path
} else {
  scriptName <- substr(scriptName, 8, nchar(scriptName))
}

pathName = substr(
  scriptName, 
  1, 
  nchar(scriptName) - nchar(strsplit(scriptName, '.*[/|\\]')[[1]][2])
)

setwd(pathName)
parent_path <- getwd()

# PART-4 OUTPUT
var_model_merged <- readRDS("../var/var_model_all_big_jan.RDS")
var_irfs_cum_merged <- readRDS("../var/var_irfs_main_all_big_jan.RDS")

var_model_merged_w <- readRDS("../var/var_model_all_big_jan_appendix.RDS")
var_irfs_cum_merged_w <- readRDS("../var/var_irfs_main_all_big_jan_appendix.RDS")


##########################################################################################
# 3) Output Model Coeff.
##########################################################################################
sink(file="../tables/var_coef_daily.tex")
summary(var_model_merged[[5]])
sink()


stargazer(var_model_merged[[5]]$varresult$Media_SMD, var_model_merged[[5]]$varresult$Party_SVP_TW, 
          var_model_merged[[5]]$varresult$Party_SP_TW, var_model_merged[[5]]$varresult$Party_FDP_TW,
          var_model_merged[[5]]$varresult$Party_CVP_TW, var_model_merged[[5]]$varresult$Party_Grüne_TW,
          var_model_merged[[5]]$varresult$Party_GLP_TW, type = 'html', dep.var.caption = "", report = c("vc*"),
          dep.var.labels = c("Equation Media\tEquation SVP\tEquation SP\tEquation FDP\tEquation CVP\tEquation Greens\tEquation GLP"),
          covariate.labels = c("Media (l1)", "FDP (l1)", "GLP (l1)", "Greens (l1)", "SP (l1)", "SVP (l1)", "CVP (l1)",
                               "Media (l2)", "FDP (l2)", "GLP (l2)", "Greens (l2)", "SP (l2)", "SVP (l2)", "CVP (l2)",
                               "Media (l3)", "FDP (l3)", "GLP (l3)", "Greens (l3)", "SP (l3)", "SVP (l3)", "CVP (l3)",
                               "Media (l4)", "FDP (l4)", "GLP (l4)", "Greens (l4)", "SP (l4)", "SVP (l4)", "CVP (l4)",
                               "Media (l5)", "FDP (l5)", "GLP (l5)", "Greens (l5)", "SP (l5)", "SVP (l5)", "CVP (l5)",
                               "Media (l6)", "FDP (l6)", "GLP (l6)", "Greens (l6)", "SP (l6)", "SVP (l6)", "CVP (l6)",
                               "Media (l7)", "FDP (l7)", "GLP (l7)", "Greens (l7)", "SP (l7)", "SVP (l7)", "CVP (l7)"),
          model.numbers =  F, out = "../tables/tabel_A06.html", header = F)

stargazer(var_model_merged[[5]]$varresult$Media_SMD, var_model_merged[[5]]$varresult$Party_SVP_TW, 
          var_model_merged[[5]]$varresult$Party_SP_TW, var_model_merged[[5]]$varresult$Party_FDP_TW,
          var_model_merged[[5]]$varresult$Party_CVP_TW, var_model_merged[[5]]$varresult$Party_Grüne_TW,
          var_model_merged[[5]]$varresult$Party_GLP_TW, type = 'latex', dep.var.caption = "",  report = c("vc*"),
          dep.var.labels = c("Equation Media\tEquation SVP\tEquation SP\tEquation FDP\tEquation CVP\tEquation Greens\tEquation GLP"),
          covariate.labels = c("Media (l1)", "FDP (l1)", "GLP (l1)", "Greens (l1)", "SP (l1)", "SVP (l1)", "CVP (l1)",
                               "Media (l2)", "FDP (l2)", "GLP (l2)", "Greens (l2)", "SP (l2)", "SVP (l2)", "CVP (l2)",
                               "Media (l3)", "FDP (l3)", "GLP (l3)", "Greens (l3)", "SP (l3)", "SVP (l3)", "CVP (l3)",
                               "Media (l4)", "FDP (l4)", "GLP (l4)", "Greens (l4)", "SP (l4)", "SVP (l4)", "CVP (l4)",
                               "Media (l5)", "FDP (l5)", "GLP (l5)", "Greens (l5)", "SP (l5)", "SVP (l5)", "CVP (l5)",
                               "Media (l6)", "FDP (l6)", "GLP (l6)", "Greens (l6)", "SP (l6)", "SVP (l6)", "CVP (l6)",
                               "Media (l7)", "FDP (l7)", "GLP (l7)", "Greens (l7)", "SP (l7)", "SVP (l7)", "CVP (l7)"),
          model.numbers =  F, out = "../tables/table_A06.tex", header = F)


sink(file="../tables/var_coef_weekly.tex")
summary(var_model_merged_w[[5]])
sink()


stargazer(var_model_merged_w[[5]]$varresult$Media_SMD, var_model_merged_w[[5]]$varresult$Party_SVP_TW, 
          var_model_merged_w[[5]]$varresult$Party_SP_TW, var_model_merged_w[[5]]$varresult$Party_FDP_TW,
          var_model_merged_w[[5]]$varresult$Party_CVP_TW, var_model_merged_w[[5]]$varresult$Party_Grüne_TW,
          var_model_merged_w[[5]]$varresult$Party_GLP_TW, type = 'html', dep.var.caption = "", report = c("vc*"),
          dep.var.labels = c("Equation Media\tEquation SVP\tEquation SP\tEquation FDP\tEquation CVP\tEquation Greens\tEquation GLP"),
          covariate.labels = c("Media (l1)", "FDP (l1)", "GLP (l1)", "Greens (l1)", "SP (l1)", "SVP (l1)", "CVP (l1)"),
          model.numbers =  F, out = "../tables/table_A07.html", header = F)

stargazer(var_model_merged_w[[5]]$varresult$Media_SMD, var_model_merged_w[[5]]$varresult$Party_SVP_TW, 
          var_model_merged_w[[5]]$varresult$Party_SP_TW, var_model_merged_w[[5]]$varresult$Party_FDP_TW,
          var_model_merged_w[[5]]$varresult$Party_CVP_TW, var_model_merged_w[[5]]$varresult$Party_Grüne_TW,
          var_model_merged_w[[5]]$varresult$Party_GLP_TW, type = 'latex', dep.var.caption = "", report = c("vc*"),
          dep.var.labels = c("Equation Media\tEquation SVP\tEquation SP\tEquation FDP\tEquation CVP\tEquation Greens\tEquation GLP"),
          covariate.labels = c("Media (l1)", "FDP (l1)", "GLP (l1)", "Greens (l1)", "SP (l1)", "SVP (l1)", "CVP (l1)"),
          model.numbers =  F, out = "../tables/table_A07.tex", header = F)
